﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using GCC.APIContainer.Utilities;
using GCC.Container.Abstracts;
using GCC.Container.Models;

using Microsoft.AspNetCore.Mvc;

using tcm.entity;
using tcm.Filters;
using tcm.request;
using tcm.request.System;
using tcm.utils;

namespace tcm.Controller.SystemManage
{
    [ApiController]
    [Route("tcm/log")]
    public class SysLogController : GCCAPIControllerBase
    {  
        public SysLogController(AbsGCCContext context) : base(context)
        {
        }


        /// <summary>
        /// 日志列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getList")]
        [TCMAuth(permission: "system:log:list")]
        public DataResponse<List<SysLogInfo>> GetList([FromQuery] SysLogRequest request)
        {
            var query = GCCEntrance.DBManager!.LocalDB!.Select<SysLogInfo>()
                     .WhereIf(!string.IsNullOrWhiteSpace(request.content), item => item.Text.Contains(request.content!))
                     .WhereIf(request.level.HasValue, item => item.Level==request.level.Value!);

            if (request.start_date.HasValue)
            {
                query=query.Where(x => x.Created>=request.start_date.Value);
            }
            if (request.end_date.HasValue)
            {
                query=query.Where(x => x.Created<=request.end_date.Value);
            }

            query= query.Count(out var count)
                      .Page(request.PageIndex ?? 1, request.PageSize)
                      .OrderByDescending(x=>x.Created);
              
            var list=query.ToList();

            return HandleData(list, count);
        }
   
    }
}
